package com.lzh.financial.util;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.ObjectUtil;
import com.lzh.financial.bean.PpayBean;
import org.springframework.data.relational.core.sql.In;

import java.time.Month;
import java.time.Year;
import java.util.*;
import java.util.stream.Collectors;

/**
 * @author reborn0530
 * @date 2021/4/4 14:18
 */
public class PayMoneyUtil {

    public static Map setStatistcs(List<PpayBean> list){
        Map<String, String> map = new LinkedHashMap<>();
        if (ObjectUtil.isNotEmpty(list)) {
            PpayBean ppayBean1 = list.get(0);
            PpayBean ppayBean2 = list.get(list.size()-1);
//        int value = Year.now().getValue();
            Integer year1 = ppayBean1.getPyear();
            Integer year2 = ppayBean2.getPyear();
            Integer month1 = ppayBean1.getPmonth();
            Integer month2 = ppayBean2.getPmonth();
            int value=12;
            for (int i=year1;i<=year2;i++){
                if (i ==year2) {
                    value=month2;
                    month1=1;
                }
                for (int a = month1; a <= value; a++) {
                    map.put(i+"-"+a,"0");
                }
            }
            for (PpayBean ppayBean : list) {
                map.put(ppayBean.getPyear()+"-"+ppayBean.getPmonth(),ppayBean.getAllMoney().toString());
            }
        }else {
            map.put(DateUtil.date().year()+"-"+DateUtil.date().month(),"0");
        }

      return map;
    }


    public static Map getSums(Map positive,Map negative) {
        Set set1 = positive.keySet();
        Set set2 = negative.keySet();
        Iterator iterator1 = set1.iterator();
        Iterator iterator2 = set2.iterator();
        Map<String, String> hashMap = new LinkedHashMap<>();
        while (iterator1.hasNext()) {
            String key = (String) iterator1.next();
            //正值
            Object value = positive.get(key);
            //负值
            Object o = negative.get(key);
            int i = Integer.valueOf(value==null?"0":value.toString()) - Integer.valueOf(o==null?"0":o.toString());
            hashMap.put(key, String.valueOf(i));
        }
        while (iterator2.hasNext()) {
            String key = (String) iterator2.next();
            //正值
            Object value = positive.get(key);
            //负值
            Object o = negative.get(key);
            int i = Integer.valueOf(value==null?"0":value.toString()) - Integer.valueOf(o==null?"0":o.toString());
            hashMap.put(key, String.valueOf(i));
        }
        return hashMap;
    }

    public static Map mapConverList(Map map) {
        Map<String, List<String>> hashMap = new HashMap<>();
        hashMap.put("name", (List<String>) map.keySet().stream().collect(Collectors.toList()));
        List<String> values=  (List<String>) map.values().stream().collect(Collectors.toList());
        hashMap.put("value",  values);
        return hashMap;
    }

}
